package fr.uha.ensisa.kfetmanager.product.admin;

import fr.uha.ensisa.kfetmanager.product.Product;
import fr.uha.ensisa.kfetmanager.product.ProductManagerRemote;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author JC
 */
public class AdminProductsCreate extends HttpServlet {
    @EJB
    private ProductManagerRemote productManager;

    private final String errorPage = "/error.jsp";
    private final String create = "/admin/products/create";
    private final String home = "/admin/products";
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        // If the method is GET, forward request to AdminProducts.jsp to display the create form.
        // If the method is POST, get all the parameters and perform actual create

        if (request.getMethod().equals("GET")) {
            this.getServletContext().setAttribute("action", "create");
            this.getServletContext().getRequestDispatcher("/admin/AdminProducts.jsp").forward(request, response);

        } else {
            String name = request.getParameter("name");
            String imageUrl = request.getParameter("imageUrl");
            String priceString = request.getParameter("price");

            // Check if all parameters have been specified
            if (name == null || imageUrl == null || priceString == null) {
                this.getServletContext().setAttribute("errorMessage", "Un param&egrave;tre n'a pas &eacute;t&eacute; sp&eacute;cifi&eacute;");
                this.getServletContext().setAttribute("redirectTo", create);
                this.getServletContext().getRequestDispatcher(errorPage).forward(request, response);
                return;
            }

            // Check if any parameter is empty String
            if (name.isEmpty() || imageUrl.isEmpty() || priceString.isEmpty()) {
                this.getServletContext().setAttribute("errorMessage", "Un param&egrave;tre est vide");
                this.getServletContext().setAttribute("redirectTo", create);
                this.getServletContext().getRequestDispatcher(errorPage).forward(request, response);
                return;
            }

            // Parse price parameter
            double price;
            try {
                price = Double.parseDouble(priceString);
            } catch (Exception e) {
                this.getServletContext().setAttribute("errorMessage", "Prix invalide");
                this.getServletContext().setAttribute("redirectTo", create);
                this.getServletContext().getRequestDispatcher(errorPage).forward(request, response);
                return;
            }

            // Create product
            try {
                productManager.createProduct(new Product(), name, price, imageUrl);
            } catch (Exception e) {
                this.getServletContext().setAttribute("errorMessage", "Erreur lors de la cr&eacute;ation de produit");
                this.getServletContext().setAttribute("redirectTo", create);
                this.getServletContext().getRequestDispatcher(errorPage).forward(request, response);
                return;
            }

            // Redirect to home page
            response.sendRedirect(getServletContext().getContextPath() + home);
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
